﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.XPath;
using System.Xml;
using System.IO;
using System.Xml.Xsl;
using System.Text.RegularExpressions;

namespace PM.PMSys
{
    /// <summary>
    /// guestbook 的摘要说明
    /// </summary>
    public class guestbook
    {
        public guestbook()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }
        /// <summary>
        /// 返回记录的总条数
        /// </summary>
        /// <returns>记录条数</returns>
        public string ReturnCount()
        {
            string i = "1";
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(HttpContext.Current.Server.MapPath("~/news/xmldb/XMLdb.xml"));
            XmlNode xmlNode = xmlDoc.DocumentElement.LastChild;
            if (xmlNode != null)
            {
                i = Convert.ToString(Convert.ToUInt32(xmlNode["id"].InnerText) + 1);
            }
            return i;
        }
        /// <summary>
        /// 增加一条新记录
        /// </summary>
        /// <param name="name">姓名</param>
        /// <param name="email">邮箱</param>
        /// <param name="qq">qq</param>
        /// <param name="picurl">头像</param>
        /// <param name="comment">留言</param>
        /// <returns>成功true失败false</returns>
        public bool AddComment(string name,string email,string qq,string picurl,string comment)
        {
            try
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(HttpContext.Current.Server.MapPath("~/news/xmldb/XMLdb.xml"));
                XmlElement newElement = xmlDoc.CreateElement("liuyan");
                XmlElement elid = xmlDoc.CreateElement("id");
                XmlElement elname = xmlDoc.CreateElement("name");
                XmlElement elemail = xmlDoc.CreateElement("email");
                XmlElement elqq = xmlDoc.CreateElement("qq");
                XmlElement elpicurl = xmlDoc.CreateElement("picurl");
                XmlElement elcomment = xmlDoc.CreateElement("comment");
                XmlElement elreply = xmlDoc.CreateElement("reply");
                XmlElement eldatetime = xmlDoc.CreateElement("datetime");
                XmlElement elstatus = xmlDoc.CreateElement("status");
                elid.InnerText = ReturnCount();
                elname.InnerText = name;
                elemail.InnerText = email;
                elqq.InnerText = qq;
                elpicurl.InnerText = picurl;
                elcomment.InnerText = comment;
                elreply.InnerText = " ";
                eldatetime.InnerText = DateTime.Now.ToString();
                elstatus.InnerText = "0";
                newElement.AppendChild(elid);
                newElement.AppendChild(elname);
                newElement.AppendChild(elemail);
                newElement.AppendChild(elqq);
                newElement.AppendChild(elpicurl);
                newElement.AppendChild(elcomment);
                newElement.AppendChild(elreply);
                newElement.AppendChild(eldatetime);
                newElement.AppendChild(elstatus);
                xmlDoc.DocumentElement.AppendChild(newElement);
                xmlDoc.Save(HttpContext.Current.Server.MapPath("~/news/xmldb/XMLdb.xml"));
                return true;
            }
            catch
            {
                return false;
            }
        }
        /// <summary>
        /// 删除一条留言
        /// </summary>
        /// <param name="id">留言id</param>
        /// <returns>成功true失败false</returns>
        public bool DeleteComment(string id)
        {
            try
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(HttpContext.Current.Server.MapPath("~/news/xmldb/XMLdb.xml"));
                XmlNodeList xmlNodeList = xmlDoc.SelectNodes("guestbook/liuyan[id='" + id + "']");
                XmlNode xmlNode = xmlNodeList.Item(0);
                xmlNode.ParentNode.RemoveChild(xmlNode);
                xmlDoc.Save(HttpContext.Current.Server.MapPath("~/news/xmldb/XMLdb.xml"));
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 通过留言审核
        /// </summary>
        /// <param name="id">留言id</param>
        /// <returns>成功true失败false</returns>
        public bool PassComment(string id)
        {
            try
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(HttpContext.Current.Server.MapPath("~/news/xmldb/XMLdb.xml"));
                XmlNodeList xmlNodeList = xmlDoc.SelectNodes("guestbook/liuyan[id='" + id + "']");//查找
                XmlNode xmlNode = xmlNodeList.Item(0);
                xmlNode["status"].InnerText = "1";
                xmlDoc.Save(HttpContext.Current.Server.MapPath("~/news/xmldb/XMLdb.xml"));
                return true;
            }
            catch
            {
                return false;
            } 
        }
        /// <summary>
        /// 管理员回复
        /// </summary>
        /// <param name="id">留言id</param>
        /// <param name="admin">管理员姓名</param>
        /// <param name="reply">回复内容</param>
        /// <returns>成功true失败false</returns>
        public bool ReplyComment(string id,string admin ,string reply)
        {
            try
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(HttpContext.Current.Server.MapPath("~/news/xmldb/XMLdb.xml"));
                XmlNodeList xmlNodeList = xmlDoc.SelectNodes("guestbook/liuyan[id='" + id + "']");//查找
                XmlNode xmlNode = xmlNodeList.Item(0);
                xmlNode["reply"].InnerText += "<br /><span style='color:#FF0000;'>-----------------------------</span><br /><span style='color:#FF0000;'>管理员【</span>" + admin + "<span style='color:#FF0000;'>】回复：</span><br />" + reply;
                xmlDoc.Save(HttpContext.Current.Server.MapPath("~/news/xmldb/XMLdb.xml"));
                return true;
            }
            catch
            {
                return false;
            } 
        }
        /// <summary>
        /// 返回留言内容
        /// </summary>
        /// <param name="id">留言id</param>
        /// <returns></returns>
        public string ReturnComment(string id)
        {
            try
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(HttpContext.Current.Server.MapPath("~/news/xmldb/XMLdb.xml"));
                XmlNodeList xmlNodeList = xmlDoc.SelectNodes("guestbook/liuyan[id='" + id + "']");//查找
                XmlNode xmlNode = xmlNodeList.Item(0);
                return xmlNode["comment"].InnerText.ToString();
            }
            catch
            {
                return " ";
            } 
        }
    }
}
